package development.app;

import state.GameConstants;
import state.GameForm;
import state.Graphics;
import state.Key;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import development.DrawUtils;
import development.Global;
import development.group.FrmGroupBongDaXoSo;
import development.group.FrmGroupThoiTietTinTuc;

public class FrmPhaseTypeMenu extends GameForm {

  private ImageText text8;
  private int chooseIndex;
  private int dy;
  private Hashtable parameters;
  private static GameForm singleton;
  
  private static final String PLAYING = "1";
  private static final String STOPPED = "0";

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmPhaseTypeMenu();
    }
    return singleton;
  }

  public void init(Hashtable parameters1) {
    this.parameters = parameters1;
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    dy = DrawUtils.getTopBarHeight();
    
    DrawUtils.setBottomBar(_manager, "", "Chọn", "Trở về");
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);
    drawPhaseType(g);
    
    DrawUtils.drawTopBar(g);
    text8.drawString2(g, "Danh sách chương trình", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 5, GameConstants.TOP_HCENTER_ANCHOR);
  }
  
  private void drawPhaseType(Graphics g){
    int textY = dy;
    if(chooseIndex == 0){
      g.setColor(0x53A4CF);
      g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, Global.getDefaultAvatar().getHeight() + 5);
    }
    g.drawImage(Global.getDefaultAvatar(), 0, textY, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, "Các chương trình đang diễn ra", GameColor.YELLOW_CODE, Global.getDefaultAvatar().getWidth() + 10, textY, GameConstants.TOP_LEFT_ANCHOR);
    textY = textY + Global.getDefaultAvatar().getHeight() + 5;
    g.setColor(0xC0C0C0);
    g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
    textY = textY + 10;
    if(chooseIndex == 1){
      g.setColor(0x53A4CF);
      g.fillRect(0, textY - 9, GameConstants.SCREEN_WIDTH, Global.getDefaultAvatar().getHeight() + 15);
    }
    g.drawImage(Global.getDefaultAvatar(), 0, textY, GameConstants.TOP_LEFT_ANCHOR);
    textY = textY + 15;
    text8.drawString2(g, "Các chương trình đã kết thúc", GameColor.YELLOW_CODE, Global.getDefaultAvatar().getWidth() + 10, textY, GameConstants.TOP_LEFT_ANCHOR);
    g.setColor(0xC0C0C0);
    textY = textY + Global.getDefaultAvatar().getHeight() - 10;
    g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.DOWN:
      if (chooseIndex < 1) {
        chooseIndex++;
      }
      break;
    case Key.UP:
      if (chooseIndex == 0) {

      }
      if (chooseIndex == 1) {
        chooseIndex--;
      }
      break;
    case Key.FIRE:
      if(chooseIndex == 0){
        parameters.put("status", PLAYING);
      }
      else{
        parameters.put("status", STOPPED);
      }
      FrmGroupBongDaXoSo.tabIndex = 1;
      Global.nextState(FrmGroupBongDaXoSo.instance(), parameters);
      break;
    case Key.SOFT_RIGHT:
      String requestType = (String) parameters.get("RequestType");
      if (requestType.equals("FOOTBALL")) {
        FrmGroupBongDaXoSo.tabIndex = 1;
        Global.nextState(FrmGroupBongDaXoSo.instance(), parameters);
      }
      if (requestType.equals("PHANG_ONLINE")) {
        FrmGroupBongDaXoSo.tabIndex = 0;
        Global.nextState(FrmGroupBongDaXoSo.instance(), parameters);
      }
      if (requestType.equals("DAU_GIA_NGUOC")) {
        FrmGroupThoiTietTinTuc.tabIndex = 2;
        Global.nextState(FrmGroupThoiTietTinTuc.instance(), parameters);
      }
      break;
    }
  }

  public void destroy() {
    parameters = null;
    text8 = null;
    chooseIndex = 0;
  }
}
